<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<view class="container-header flex-direction-column align-items-center">
			<image class="header-logo" :src="appletLogo" mode="aspectFit"></image>
			<view class="header-name">{{appletName}}</view>
		</view>
		<view class="container-login">
			<!-- <view class="login-title">小程序需要登录注册才能使用相关功能，申请获取以下权限</view>
			<view class="login-subtitle">获得你的公开信息(昵称、头像、手机号码等)</view> -->
			<!-- 授权登录 -->
			<view class="login-info" v-if="isRegister">
				<view class="info-btn" @click="handleLogin">授权登录</view>
				<view class="info-btn cancel" @click="goBack">暂不登录</view>
			</view>
			<!-- 授权手机号 -->
			<view class="login-info" v-else-if="!phoneNumber">
				<button class="info-btn" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">授权手机号</button>
				<view class="info-btn cancel" @click="goBack">暂不授权</view>
			</view>
			<!-- 授权头像昵称 -->
			<form class="login-info" @submit="handleLogin" v-else>
				<view class="info-item">
					<view class="item-title">头像</view>
					<button class="item-avatar" open-type="chooseAvatar" @chooseavatar="bindChooseAvatar">
						<image class="image" :src="avatarPath || '/static/login.png'" mode="aspectFill"></image>
					</button>
				</view>
				<view class="info-item">
					<view class="item-title">昵称</view>
					<input class="item-nickname" type="nickname" name="nickname" :value="userInfo.nickname"
						placeholder="请输入昵称" />
				</view>
				<button class="info-btn" style="margin-top: 48rpx;" form-type="submit">授权登录</button>
				<button class="info-btn cancel" @click="loginEvent">稍后授权</button>
			</form>
		</view>
	</view>
</template>
<script>
	var app = getApp( )
	import ikz from '@/pages/Common/tools.js'
	export default {
		data( ) {
			return {
				// 加载完成
				loadEnd: false,
				// 是否已经注册
				isRegister: false,
				// 已授权手机号
				phoneNumber: null,
				// 已选择头像
				avatarPath: "",
				// 用户信息
				userInfo: {
					avatar: "",
					nickname: "",
				},
				themeColor: "#ffc045",
				appletLogo: getApp( ).globalData.adminImgPath + getApp( ).globalData.loginLogo,
				appletName: "小七精灵万能跑腿",
				token: "",
				privacyAgreed: false
			}
		},
		onLoad( ) {
			uni.showLoading( {
				title: "加载中"
			} )
			this.getAuthStatus( ( ) => {
				uni.hideLoading( )
				this.loadEnd = true
			} )
		},
		methods: {
			// 获取账号是否已注册
			getAuthStatus( fn ) {
				uni.login( {
					provider: 'weixin',
					success: loginRes => {
						var path = '/xyb/user/is_register';
						var data = {
							code: loginRes.code
						};
						ikz.request( path, data, res => {
							if ( fn ) fn( )
							if ( res.data.code == 1 ) {
								if ( res.data.data.auth_status == 1 ) {
									this.isRegister = true
								} else {
									this.isRegister = false
								}
							} else {
								uni.showModal( {
									title: '提示',
									content: '服务器繁忙，请稍后再试',
									showCancel: false,
									success: function( res ) {}
								} );
							}

						} );
					},
					fail: ( ) => {
						uni.showToast( {
							icon: "none",
							title: "授权失败，请重试"
						} )
					}
				} )
			},
			// 返回上一页
			goBack( ) {
				uni.switchTab( {
					url: "/pages/Personal/index"
				} )
				// if (getCurrentPages().length == 1) {
				// 	uni.switchTab({
				// 		url: "/pages/Personal/index"
				// 	})
				// } else {
				// 	uni.navigateBack()
				// }
			},
			// 获取用户手机号码
			decryptPhoneNumber( e ) {
				console.log( e, 'eeee' );
				if ( e.detail.errMsg == "getPhoneNumber:ok" ) {
					uni.showLoading( {
						title: "加载中",
						mask: true
					} )
					wx.requirePrivacyAuthorize( {
						success: ( ) => {
							uni.login( {
								provider: 'weixin',
								success: loginRes => {
									let data = e.detail
									data.code = loginRes.code
									var path = '/xyb/user/get_phone';
									ikz.request( path, data, res => {
										if ( res.data.code == 1 ) {
											uni.hideLoading( )
											this.phoneNumber = res.data.data.phoneNumber
											getApp( ).globalData.mobile = res.data.data
												.phoneNumber
										} else {
											uni.showToast( {
												title: res.data.msg,
												icon: 'none'
											} )
										}

									} );
								},
								fail: ( ) => {
									uni.hideLoading( )
									uni.showToast( {
										icon: "none",
										title: "授权失败，请重试"
									} )
								}
							} );
						}
					} )
					fail: ( ) => {
						uni.hideLoading( )
						uni.showToast( {
							title: "需要同意隐私协议才能使用",
							icon: 'none'
						} )
					}


				} else {
					uni.showToast( {
						title: '获取失败，请重新获取',
						icon: 'none'
					} )
				}
			},
			// 选择头像  
			bindChooseAvatar( e ) {
				this.avatarPath = e.detail.avatarUrl
			},
			// 提交登录
			handleLogin( e ) {
				if ( !this.isRegister ) {
					this.userInfo.nickname = e.detail.value.nickname
					if ( !this.avatarPath ) {
						uni.showToast( {
							title: "请设置头像后登录",
							icon: 'none',
						} )
						return
					}
					if ( !this.userInfo.nickname ) {
						uni.showToast( {
							title: "请设置昵称后登录",
							icon: 'none',
						} )
						return
					}
				}
				uni.showLoading( {
					title: '授权中',
					mask: true,
				} )
				if ( this.avatarPath ) {
					uni.uploadFile( {
						url: app.globalData.adminPath + '/xyb/common/upload',
						filePath: this.avatarPath,
						name: 'file',
						formData: {},
						success: uploadFileRes => {
							this.userInfo.avatar = JSON.parse( uploadFileRes.data ).data.url
							this.loginEvent( )
						},
						complete: ( ) => {
							uni.hideLoading( )
						},
					} )
				} else {
					this.loginEvent( )
				}
			},
			// 提交事件
			loginEvent( ) {
				uni.login( {
					provider: 'weixin',
					success: loginRes => {
						let path = ""
						let data = {
							code: loginRes.code
						}
						if ( this.isRegister ) {
							path = "/xyb/user/login"
						} else {
							path = "/xyb/user/register"
							data.mobile = this.phoneNumber
							if ( uni.getStorageSync( "parentWechatId" ) ) data.parent_wechat_id = uni
								.getStorageSync( "parentWechatId" )
							if ( this.userInfo.nickname ) data.nickname = this.userInfo.nickname
							if ( this.userInfo.avatar ) data.avatar = this.userInfo.avatar
						}
						ikz.requestPost( path, data, res => {
							if ( res.data.code == 1 ) {
								uni.setStorageSync( 'local_user_session', res.data.data.token );
								let user_info = {
									nickname: res.data.data.nickname,
									avatar: res.data.data.avatar,
									mobile: res.data.data.mobile,
									agent_area:res.data.data.agent_area
								}
								uni.setStorageSync( "user_info", user_info )
								this.goBack( )
							} else {
								uni.showToast( {
									title: res.data.msg,
									icon: 'none'
								} )
							}

						} );
					},
					fail: ( ) => {
						uni.showToast( {
							icon: "none",
							title: "授权失败，请重试"
						} )
					}
				} )
			},
			// 稍后授权
			laterAuthorize( ) {
				uni.showLoading( {
					title: '授权中',
					mask: true,
				} )
				this.loginEvent( )
			},
		}
	}
</script>
<style lang="scss">
	page {
		background: #fff;
	}

	.container {
		padding: 96rpx 64rpx 0;

		.container-header {
			.header-logo {
				width: 300rpx;
				height: 300rpx;
			}

			.header-name {
				color: #5A5B6E;
				font-size: 48rpx;
				font-weight: 600;
				line-height: 48rpx;
				margin-top: 48rpx;
			}
		}

		.container-login {
			margin-top: 76rpx;

			.login-title {
				color: #585858;
				font-size: 34rpx;
			}

			.login-subtitle {
				color: #888;
				font-size: 28rpx;
				margin-top: 40rpx;
			}

			.login-info {
				margin-top: 48rpx;

				.info-item {
					display: flex;
					align-items: center;
					background: #ffffff;
					border-bottom: 1rpx solid #F6F7FB;

					.item-title {
						margin-right: 40rpx;
						font-size: 28rpx;
						line-height: 40rpx;
						color: #5A5B6E;
					}

					.item-avatar {
						flex: 1;
						padding: 32rpx 0;
						height: 80rpx;
						background: transparent;
						box-sizing: content-box;
						display: flex;
						justify-content: flex-start;

						&::after {
							border: none;
						}

						.image {
							width: 80rpx;
							height: 80rpx;
							border-radius: 50%;
						}
					}

					.item-nickname {
						flex: 1;
						font-size: 28rpx;
						line-height: 40rpx;
						padding: 32rpx 0;
						box-sizing: content-box;
					}
				}

				.info-btn {
					font-size: 28rpx;
					line-height: 40rpx;
					color: #ffffff;
					text-align: center;
					padding: 36rpx 32rpx;
					border-radius: 16rpx;
					background: var(--theme-color);

					&.cancel {
						background: #F6F7FB;
						color: var(--theme-color);
						margin-top: 32rpx;
					}
				}
			}
		}
	}
</style>